import re
from wtforms import Form, StringField, IntegerField
from wtforms.validators import DataRequired, length, NumberRange
from flask import request

from app.libs.error_code import ClientPostError


class BookSearchForm(Form):
    key = StringField(validators=[DataRequired(), length(min=1, max=13)])
    start = IntegerField(validators=[NumberRange(min=0, max=99)], default=0)
    count = IntegerField(default=15)

    def validate_key(self, field):
        v = str(field).lower()
        pattern = r"\b(and|like|exec|insert|select|drop|grant|alter|delete|update|count|chr|mid|master|truncate|char|delclare|or)\b|(\*|;|%)"
        r = re.search(pattern, v)
        if r:
            raise ClientPostError()

